package com.hxbd.gxsec_data.controller;

import com.alibaba.fastjson.JSONObject;
import com.hxbd.gxsec_data.pojo.FileDO;
import com.hxbd.gxsec_data.pojo.ResourceDO;
import com.hxbd.gxsec_data.service.ResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.security.PermitAll;
import java.util.List;
import java.util.Map;

@Controller
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping(value = "/uploadFiles")
    @ResponseBody
    public FileDO uploadFiles(@RequestBody MultipartFile file){
        return resourceService.uploadFiles(file);
    }

    /**
     * 保存资源
     * @param param
     * @return
     */
    @PostMapping(value = "/saveAddResouce")
    @ResponseBody
    public boolean saveAddResouce(@RequestParam Map<String,Object> param){
        return resourceService.saveAddResouce(param);
    }

    /**
     * 获取全部资源列表
     * @return
     */
    @GetMapping(value = "/getAllResource")
    @ResponseBody
    public List<JSONObject> getAllResource(){
        return resourceService.getAllResource();
    }

    /**
     * 删除资源
     * @param resourceId
     * @return
     */
    @GetMapping(value = "/deleteResource")
    public String deleteResource(@RequestParam Map<String,String> map){
        return resourceService.deleteresource(map.get("resourceId"),map.get("fileName"))?"resourceManager":null;
    }

    /**
     * 统计进入下载页面次数
     * @param
     * @return
     */
    @GetMapping(value = "/countTheNumberOfPageVisits")
    @ResponseBody
    public Integer countTheNumberOfPageVisits(@RequestParam String resourceId){
        return resourceService.countTheNumberOfPageVisits(resourceId);
    }

    /**
     * 搜索
     * @param name
     * @return
     */
    @GetMapping(value = "/searchResource")
    @ResponseBody
    public List<JSONObject> searchResource(@RequestParam String name){
        return resourceService.searchResource(name);
    }


}
